/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.only5.manager.impl;

import edu.uoc.tdp.pac4.only5.configuration.IConfiguration;
import edu.uoc.tdp.pac4.only5.configuration.PropertiesConfiguration;
import edu.uoc.tdp.pac4.only5.manager.DatabaseConnectionManager;
import edu.uoc.tdp.pac4.only5.exceptions.EespaisDatabaseException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Josep
 * @author Gerard
 */
public class DataBaseConnectionManagerImpl implements DatabaseConnectionManager {

    private static DataBaseConnectionManagerImpl instance = new DataBaseConnectionManagerImpl();
    
    private IConfiguration configuration;
    private Connection connection;

    private DataBaseConnectionManagerImpl() {
        this.configuration = PropertiesConfiguration.getInstance();
    }

    public static DataBaseConnectionManagerImpl getInstance() {
        return instance;
    }

    @Override
    public void openConnection() throws EespaisDatabaseException {
        String url = configuration.getProperty(IConfiguration.DATABASE_URL);
        String user = configuration.getProperty(IConfiguration.DATABASE_USER);
        String passwd = configuration.getProperty(IConfiguration.DATABASE_PASSWD);
        try {
            connection = DriverManager.getConnection(url, user, passwd);
        } catch (SQLException ex) {
            throw new EespaisDatabaseException(ex.getMessage(), ex);
        }
    }

    @Override
    public Statement createStatement() throws EespaisDatabaseException {
        Statement statement = null;
        try {
            statement = connection.createStatement();
        } catch (SQLException ex) {
            throw new EespaisDatabaseException(ex.getMessage(), ex);
        }
        return statement;
    }

    @Override
    public PreparedStatement createPreparedStatement(String prepStatement) throws EespaisDatabaseException {
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(prepStatement, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException ex) {
            throw new EespaisDatabaseException(ex.getMessage(), ex);
        }
        return preparedStatement;
    }

    @Override
    public void closeConnection() throws EespaisDatabaseException {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            throw new EespaisDatabaseException(ex.getMessage(), ex);
        }
    }

    @Override
    public Connection getConnection() {
        return connection;
    }

    @Override
    public void commit() throws EespaisDatabaseException {
        try {
            connection.commit();
        } catch (SQLException ex) {
            throw new EespaisDatabaseException(ex.getMessage(), ex);
        }
        
        
    }

    @Override
    public void rollback() throws EespaisDatabaseException  {
        try {
            connection.rollback();
        } catch (SQLException ex) {
           throw new EespaisDatabaseException(ex.getMessage(), ex);
        }
    }
    
    public void setAutoCommit(boolean autoCommit) throws EespaisDatabaseException {
        try {
            connection.setAutoCommit(autoCommit);
        } catch (SQLException ex) {
            throw new EespaisDatabaseException(ex.getMessage(), ex);
        }
    }
}